<?php

namespace App\Models;

use App\Http\Controllers\Admin\CommonController;
use Exception;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;

/**
 * 图书到家书单推荐
 */
class BookHomeReadingList extends BaseModel
{
    use HasFactory;

    const CREATED_AT = 'create_time';
    const UPDATED_AT = 'change_time';


    protected $table = 'book_home_reading_list';


    /*关联书单书籍*/
    public function conReadingListShopBook()
    {
        return $this->belongsToMany(ShopBook::class, BookHomeReadingListBook::class, 'reading_id', 'book_id');
    }


    /*关联书单书籍*/
    public function conReadingListLibBook()
    {
        return $this->belongsToMany(LibBookRecom::class, BookHomeReadingListBook::class, 'reading_id', 'book_id');
    }

    /**
     *获取推荐的书单
     */
    public function getRecomReadingShopList($shop_id = null, $limit = 3)
    {
        $res = $this->select('id', 'name', 'intro', 'type', 'create_time')
            ->with(['conReadingListShopBook' => function ($query) {
                $query->select('shop_book.id', 'book_name', 'author', 'press', 'pre_time', 'isbn', 'price', 'img', 'intro')->limit(5);
            }])
            ->whereHas('conReadingListShopBook', function ($query) {
                $query->where('shop_book.is_del', 1); //删除掉不存在的书籍数据，同时 如果一本都不存在，则不返回当前书单
            })
            ->withCount('conReadingListShopBook') //统计数量
            ->where(function ($query) use ($shop_id) {
                if ($shop_id) {
                    $query->where('shop_id', $shop_id);
                }
            })
            ->where('type', 1)
            ->where('is_del', 1)
            ->orderByDesc('id')
            ->paginate($limit)
            ->toArray();

        return $res;
    }


    /**
     *获取推荐的书单
     */
    public function getRecomReadingLibList($limit = 3)
    {
        $res = $this->select('id', 'name', 'intro', 'type', 'create_time')
            ->with(['conReadingListLibBook' => function ($query) {
                $query->select('lib_book_recom.id', 'metaid', 'metatable', 'book_name', 'author', 'press', 'pre_time', 'isbn', 'price', 'img', 'intro');
            }])
            ->whereHas('conReadingListLibBook', function ($query) {
                $query->where('lib_book_recom.is_del', 1); //删除掉不存在的书籍数据，同时 如果一本都不存在，则不返回当前书单
            })
            ->where('type', 2)
            ->where('is_del', 1)
            ->orderByDesc('id')
            ->paginate($limit)
            ->toArray();
        return $res;
    }

    /**
     *获取列表
     * @param limit int 分页大小
     * @param type    类型 1 新书 2馆藏书
     * @param keywords string 搜索关键词
     */
    public function lists($search_data = [])
    {
        $keywords = $search_data['keywords'] ?? '';
        $type = $search_data['type'] ?? '';
        $shop_id = !empty($search_data['shop_id']) ? $search_data['shop_id'] : '';
        $limit = !empty($search_data['limit']) ? $search_data['limit'] : 10;
        $shop_all_id = !empty($search_data['shop_all_id']) ? $search_data['shop_all_id'] : '';


        $res = $this->select('id', 'name', 'intro', 'type', 'shop_id', 'create_time')
            ->where(function ($query) use ($keywords) {
                if ($keywords) {
                    $query->where('name', 'like', "%$keywords%");
                }
            })->where(function ($query) use ($type, $shop_id, $shop_all_id) {
                if ($type) {
                    $query->where('type', $type);
                }
                if ($shop_id) {
                    $query->where('shop_id', $shop_id);
                }
                if ($shop_all_id) {
                    $query->whereIn('shop_id', $shop_all_id);
                }
            })
            ->where('is_del', 1)
            ->orderByDesc('id')
            ->paginate($limit)
            ->toArray();

        return $res;
    }
    /**
     * 详情
     * @param id
     */
    public function detail($id)
    {
        $res = $this->select('id', 'name', 'intro', 'type', 'shop_id', 'create_time')
            ->where('id', $id)->first();
        if (empty($res)) {
            return false;
        }
        return $res;
    }
}
